<?php
if (!defined ('TYPO3_MODE')) 	die ('Access denied.');

$TCA["user_farm_commerce_product_branch_mm"] = array (
	"ctrl" => $TCA["user_farm_commerce_product_branch_mm"]["ctrl"],
	"interface" => array (
		"showRecordFieldList" => "branch_id,branch_price,branch_price_opt,rest"
	),
	"feInterface" => $TCA["user_farmcommerce_product_branch_mm"]["feInterface"],
	"columns" => array (
		"product_id" => Array (
			"exclude" => 1,
			"config" => Array (
				"type" => "passthrough"
			)
		),
		"branch_id" => Array (		
			"exclude" => 1,		
			"label" => "LLL:EXT:user_farm_commerce/locallang_db.xml:user_farmcommerce_product_branch_mm.branch_id",		
			"config" => Array (
				"type" => "select",	
				"foreign_table" => "user_farm_branches",	
				"foreign_table_where" => "ORDER BY user_farm_branches.name",	
				"size" => 1,	
				"minitems" => 0,
				"maxitems" => 1,
			)
		),
		"branch_price" => Array (		
			"exclude" => 1,		
			"label" => "LLL:EXT:user_farm_commerce/locallang_db.xml:user_farmcommerce_product_branch_mm.branch_price",		
			"config" => Array (
				"type" => "input",	
				"size" => "15",	
				"max" => "255",	
				"eval" => "required,trim",
			)
		),
		"branch_price_opt" => Array (		
			"exclude" => 1,		
			"label" => "LLL:EXT:user_farm_commerce/locallang_db.xml:user_farmcommerce_product_branch_mm.branch_price_opt",		
			"config" => Array (
				"type" => "input",	
				"size" => "15",	
				"max" => "255",	
				"eval" => "required,trim",
			)
		),
		"rest" => Array (		
			"exclude" => 1,		
			"label" => "LLL:EXT:user_farm_commerce/locallang_db.xml:user_farmcommerce_product_branch_mm.rest",		
			"config" => Array (
				"type"     => "input",
				"size"     => "4",
				"max"      => "4",
				"eval"     => "int",
				"checkbox" => "0",
				"range"    => Array (
					"upper" => "1000",
					"lower" => "10"
				),
				"default" => 0
			)
		),
	),
	"types" => array (
		"0" => array("showitem" => "branch_id;;;;1-1-1, branch_price, branch_price_opt, rest")
	),
	"palettes" => array (
		"1" => array("showitem" => "")
	)
);

	//В случае если пользователь не администратор, модифицируем TCA таблицы,
	//содержащей сведения о ценах и остатках товара в филиале, так,
	//чтобы он мог редактировать записи только тех филиалов, менеджером или 
	//руководителем которых он является 
if ($GLOBALS['BE_USER'] && !$GLOBALS['BE_USER']->isAdmin())
{
	t3lib_div::loadTCA("user_farm_commerce_product_branch_mm");
	
	if ($GLOBALS['BE_USER']->groupList)
	{
		$res = $GLOBALS['TYPO3_DB']->sql_query("SELECT uid 
			FROM user_farm_branches 
			WHERE group_id IN (".$GLOBALS['BE_USER']->groupList.") OR leader_id = ".$GLOBALS['BE_USER']->user["uid"]);
		if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0)
		{
			$branches = array();
			while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
			{
				$branches[] = $row['uid']; 
			}
			
			$TCA["user_farm_commerce_product_branch_mm"]["columns"]["branch_id"]["config"]["foreign_table_where"] = "
				 AND user_farm_branches.uid IN (".implode(", ", $branches).") ORDER BY user_farm_branches.name";
		}
	}
	else
	{
		$TCA["user_farm_commerce_product_branch_mm"]["columns"]["branch_id"]["config"]["foreign_table_where"] = "
			AND 1 = 0"; 
	}	
}
?>